package com.baizhi.wwx.service;

import com.baizhi.wwx.dao.UserMapper;

import com.baizhi.wwx.entity.User;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Map<String, Object> queryAllPage(int page, int pageSize) {
        Map<String,Object> map=new HashMap<>();
        User user=new User();
        RowBounds rowBounds=new RowBounds((page-1)*pageSize,pageSize);
        List<User>users=userMapper.selectByRowBounds(user,rowBounds);
        int total=userMapper.selectCount(null);
        map.put("total",total);
        map.put("page",page);
        map.put("rows",users);
        return map;

    }
    @Transactional
    @Override
    public Map<String, Object> update(Integer id, String status) {
        Map<String,Object>map=new HashMap<>();
        User user=new User();
        user.setId(id);
        user.setStatus(status);
        int a=userMapper.updateByPrimaryKeySelective(user);
        if (a>=1){
            map.put("status",200);
            map.put("message","操作成功");
        }else {
            map.put("status",400);
            map.put("message","操作失败");
        }
        return map;
    }
    @Transactional
    @Override
    public Map<String, Object> delete(Integer id) {
        Map<String,Object> map=new HashMap<>();
        User user=new User();
        user.setId(id);
        int i = userMapper.deleteByPrimaryKey(user);
        if (i ==1){
            map.put("message","操作成功");
            map.put("status",200);
        }else {
            map.put("status",400);
            map.put("message","操作失败");
        }

        return map;
    }
}
